% Author: Vasyl Mykhalchuk
% Created: 20/04/2012

function [strains] = strain_analysis( frame_num )
% StrainAnalysis
% performs strain analysis on animated mesh (data_file_name)
% load the data from .mat file
data_file_name = 'horse.mat';
anim_data = load(data_file_name);
frames = anim_data.frames;              % vertex coordinates per frames
topology = anim_data.topology;          % connectivity
clear anim_data;
% Load vertex adjacency matrix (CCW)
vert_adj_ccw_data = load('horse_vert_adj_ccw.mat');
vert_adj_ccw = vert_adj_ccw_data.vert_adj_ccw;
clear vert_adj_ccw_data;

% Reference frame
ref_frame_vector = frames(1, :);
ref_frame = toMatrix(ref_frame_vector);
clear ref_frame_vector;

vert_num = size(ref_frame, 1);
strains = zeros(vert_num, frame_num);

for fi = frame_num : frame_num

target_frame = frames(fi, :);
target_frame = toMatrix(target_frame);


for i = 1 : vert_num
    v_initail = ref_frame(i, :);
    v_deformed = target_frame(i, :);
    [e] = strain_vert( v_initail, v_deformed, vert_adj_ccw(i, :), ref_frame, target_frame);
    strains(i, fi) = e(1);
    if (strains(i, fi) > 22)
        strains(i, fi) = 22;
    end % IF
end % FOR

end % FOR frame_num

ss = zeros(vert_num, 1);
for i = 1 : vert_num
    ss(i) = max(strains(i, :));
end % FOR

colors = colorForField( ss, -1 );
path = ['horse.ply'];
exportPLY( path, target_frame, colors, topology, 0 );


end % function

